package com.tgy.atomic;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @Author: tgy
 * @Date: 12/8/20 3:37 PM
 */
public class ABA {

    public static void main(String[] args) throws InterruptedException {


        AtomicStampedReference<Integer> stampedReference = new AtomicStampedReference<>(1, 1);

        int st = stampedReference.getStamp();
        Integer ref = stampedReference.getReference();
        new Thread(()->{

            int stamp = stampedReference.getStamp();

            Integer reference = stampedReference.getReference();

            boolean result = stampedReference.compareAndSet(reference, reference + 1, stamp, stamp + 1);

            System.out.println(result);

            int stamp1 = stampedReference.getStamp();

            Integer old = stampedReference.getReference();

            stampedReference.compareAndSet(old,reference,stamp1,stamp1 + 1);

        },"t1").start();

        TimeUnit.SECONDS.sleep(1);
        boolean res = stampedReference.compareAndSet(ref, ref + 1, st, st + 1);

        System.out.println(res);
    }
}
